package utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	/**
	 * 以位图的方式播放movieClip. 
	 * @author Administrator
	 * 
	 */
	public class VectorMovieClip extends Sprite
	{
		private var bitmaps:Vector.<BitmapData>=new Vector.<BitmapData>();
		private var bitmap:Bitmap=new Bitmap();
		private var _mc:MovieClip;
		private var _currentFrame:int=1;
		public function VectorMovieClip(mc:MovieClip,isCache:Boolean=false)
		{
			_mc=mc;
			_mc.gotoAndStop(1);
			if(isCache){
				var bit:BitmapData;
			    for(var i:int=0;i<_mc.totalFrames;i++){
				    draw();
				}
			}
			_currentFrame=1;
			play();
			addChild(bitmap);
		}
		public function stop():void{
			_mc.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		public function play():void{
			_mc.addEventListener(Event.ENTER_FRAME,onEnterFrame);
		}
		private function onEnterFrame(event:Event):void
		{
			draw();
		}
		private function draw():void{
			if(_currentFrame>=_mc.totalFrames){
				_currentFrame=1;
			}
			var bit:BitmapData
			if(_currentFrame>=bitmaps.length){
				_mc.gotoAndStop(_currentFrame);
			    bit=new BitmapData(_mc.width,_mc.height,true,0);
				bit.draw(_mc);
				bitmaps.push(bit);
			}else{
			    bit=bitmaps[_currentFrame];
			}
			
			bitmap.bitmapData=bit; 
			_currentFrame++;
		}
	}
}